<?php
/**
 * Created by PhpStorm.
 * User: mr_lee
 * Date: 2017/10/17
 * Time: 上午9:42
 */
namespace app\index\controller;
use app\common\controller\Balance;
use app\common\controller\Bonus;
use app\common\controller\TemplateMsg;
use app\common\model\CardCzLog;
use app\common\model\CardUserGet;
use app\common\model\Paylog;
use think\Controller;
class test extends Controller
{

    public function test($id){
        $id = '1632';
        $Paylog= new Paylog();
        $pay = $Paylog->where(['id'=>$id])->find();
        $updata['mid'] = $pay['mid'];
        $updata['openid'] = $pay['openid'];
        $updata['price'] = $pay['price'];
        $updata['orderid'] = $pay['orderid'];
        $updata['get_card_id'] = $pay['cz_card_id'];
        $res = $this->updata_card_infos($updata);
        var_dump($res);
    }

//$updata['mid'] = $id['mid'];
//$updata['openid'] = $id['openid'];
//$updata['price'] = $id['price'];
//$updata['orderid'] = $id['orderid'];
//$updata['get_card_id'] = $id['cz_card_id'];

    //
  
    //更新积分余额
    public function updata_card_infos($updata)
    {
        $CardUserGet = new CardUserGet();
        $CardCzLog = new CardCzLog();
        $ComBalance = new Balance();
        $ComCardPut = new \app\common\controller\CardPut();
        $ComBonus = new Bonus();
        //查询领卡记录
        $user = $CardUserGet->where('id', $updata['get_card_id'])->find();
        //余额更新
        $arr['card_id'] = $user['card_id'];
        $arr['code'] = $user['code'];
        $arr['openid'] = $updata['openid'];
        $arr['mid'] = $updata['mid'];
        $arr['giveid'] = $CardCzLog->where('orderid', $updata['orderid'])->value('giveid');
        $arr['add_balance'] = $updata['price'];
        $arr['id'] = $user['id'];
        $arr['nickname'] = 'ONLINERECH';
        //余额更新
        $arrc = $ComCardPut->checkgive($arr);
        //变动更新
        $re = $CardUserGet->where('id', $user['id'])->setInc('balance', $arrc['add_balance']);
        if ($re !== false) {
            if ($arr['giveid'] !== ''){
                $user['giveid'] = $arr['giveid'];
                $resb = $ComBonus->cs_bonus_rec($user);
            }else{
                $resb = $ComBonus->cz_bonus_rec($user,$arr);
            }
            //余额变动模版消息
            $balance = $CardUserGet->where(array('id' => $user['id']))->value('balance');
            $spa['card_id']=$user['card_id'];
            $spa['openid']=$user['openid'];
            $spa['add_balance']= $arrc['add_balance'];
            $spa['balance']=$balance;
            $spa['type']=1;
            $wxMsg = new TemplateMsg();
            $wxMsg->send_tpl('pay_balance', $spa);
            //保存变动记录
            $arr['hx_fs']=4;
            $arr['type']=1;
            $arr['cate']=2;
            $arr['reason']=3;
            $arr['source']=1;
            $arr['add_bonus'] = $resb['add_bonus'];
            $updataid = $ComBalance->cardupdate($arr, $user, $arrc);
            //订单记录
            $arr['pay_balance'] = $arrc['pay_balance'];
            $datalog = $ComBalance->paylog_rec($arr);
            //充值记录
            $give_rec_id = $ComBalance->cardczlog_rec($datalog, $user, $arrc, $arr);
            //冲送
            $arr['give_rec_id'] = $give_rec_id;
            $arr['top_mid'] = $user['top_mid'];
            $arr['updataid'] = $updataid;
            $resut = $ComBalance->cs_type($arr, $user);
        }

    }



}